import { ElMessage } from 'element-plus'

// 防抖函数
export const debounce = (fn, delay) => {
  let timer = null
  return function (...args) {
    if (timer) return
    timer = setTimeout(() => {
      fn.apply(this, args)
      timer = null
    }, delay)
  }
}

// 创建防抖的错误提示函数
const debouncedError = debounce((message) => {
  ElMessage.error(message)
}, 300)

const isEmpty = (rule, value, callback) => {
  if (value === '' || value === null || value === undefined) {
    debouncedError(rule.message)
    callback(new Error(rule.message))
  } else callback()
}

export const rulesMap = {
  logistics: {
    totalScore: [
      {
        trigger: 'blur',
        message: '总分不能为空!',
        validator: isEmpty,
        name: 'totalScore',
      },
    ],
    useTime: [
      {
        trigger: 'blur',
        message: '比赛用时不能为空!',
        validator: isEmpty,
      },
    ],
  },
  fire: {
    totalScore: [
      {
        trigger: 'blur',
        message: '总分不能为空!',
        validator: isEmpty,
        name: 'totalScore',
      },
    ],
    useTime: [
      {
        trigger: 'blur',
        message: '比赛用时不能为空!',
        validator: isEmpty,
      },
    ],
  },
  obstacle: {
    honorValue: [
      {
        trigger: 'change',
        message: '荣誉值不能为空!',
        validator: isEmpty,
        name: 'honorValue',
      },
    ],
    useTime: [
      {
        trigger: 'blur',
        message: '比赛用时不能为空!',
        validator: isEmpty,
      },
    ],
  },
  gobang: {
    sumPoint: [
      {
        trigger: 'blur',
        message: '总落子分不能为空!',
        validator: isEmpty,
        name: 'honorValue',
      },
    ],
    useTime: [
      {
        trigger: 'blur',
        message: '比赛用时不能为空!',
        validator: isEmpty,
      },
    ],
  },
  drone: {
    sumPoint: [
      {
        trigger: 'blur',
        message: '总落子分不能为空!',
        validator: isEmpty,
        name: 'honorValue',
      },
    ],
    useTime: [
      {
        trigger: 'blur',
        message: '比赛用时不能为空!',
        validator: isEmpty,
      },
    ],
  },
}

export const getRules = (type, tree) => {
  // 按照树结构生成rules，保证rules生效
  const result = []
  tree.forEach((item, index) => {
    result.push({ tableData: [] })
    if (item.tableData) {
      item.tableData.forEach((tableItem) => {
        result[index].tableData.push(rulesMap[type])
      })
    }
  })
  return result
}

// 转换为毫秒时长
export const getDuration = (dateObj) => {
  if (dateObj === null) return 0
  return (
    (dateObj.getHours() * 3600 +
      dateObj.getMinutes() * 60 +
      dateObj.getSeconds()) *
    1000
  )
}

// 从Date转换为时分秒
export const getTimeFromDate = (dateObj, ms) => {
  if (dateObj === null) return '00:00:00:000'
  return `${dateObj.getHours()}:${dateObj.getMinutes()}:${dateObj.getSeconds()}:${
    ms ?? 0
  }`
}
// 从毫秒转换回时分秒
export const getTime = (duration, ms) => {
  if (!duration) return `00:00:00:${ms || '000'}`
  let hours, minutes, seconds, milliseconds
  if (duration instanceof Date) {
    hours = duration.getHours()
    minutes = duration.getMinutes()
    seconds = duration.getSeconds()
    milliseconds = ms ?? duration.getMilliseconds()
  } else {
    hours = Math.floor(duration / (1000 * 60 * 60))
    minutes = Math.floor((duration / (1000 * 60)) % 60)
    seconds = Math.floor((duration / 1000) % 60)
    milliseconds = ms ?? Math.floor(duration % 1000)
  }
  if (hours < 10) hours = `0${hours}`
  if (minutes < 10) minutes = `0${minutes}`
  if (seconds < 10) seconds = `0${seconds}`
  if (milliseconds < 100) {
    if (milliseconds < 10) milliseconds = `00${milliseconds}`
    else milliseconds = `0${milliseconds}`
  }
  return `${hours}:${minutes}:${seconds}:${milliseconds}`
}

// 毫秒转Date
export function msToTime(duration) {
  const seconds = Math.floor((duration / 1000) % 60)
  const minutes = Math.floor((duration / (1000 * 60)) % 60)
  const hours = Math.floor(duration / (1000 * 60 * 60))

  return {
    date: new Date(0, 0, 0, hours, minutes, seconds),
    ms: Math.floor(duration % 1000),
  }
}
